import { informationReq } from '@/api/Admin/User';
import { loginReq, logoutReq, smsLoginReq } from '@/api/Auth';
import router from '@/router';
import { RootState } from '@/store/types';
import { LoginDTO, SmsLoginDTO } from '@/types/User/dto';
import { encryption } from '@/utils/encrypt';
import { ActionContext, ActionTree } from 'vuex';
import { ActionTypes } from './action-types';
import { ActionTypes as CommonActionTypes } from '../common/action-types';
import { MutationTypes } from './mutation-types';
import { UserState } from './types';
import { defaultRoutePath, loginRoutePath } from '@/const/path';

export type Actions<S = UserState, R = RootState> = {
  [ActionTypes.LOGIN_USER]({ commit }: ActionContext<S, R>, data: LoginDTO): Promise<void>
  [ActionTypes.LOGIN_USER_SMS]({ commit }: ActionContext<S, R>, data: SmsLoginDTO): Promise<void>
  [ActionTypes.GET_USER_INFORMATION]({ commit }: ActionContext<S, R>): Promise<void>
  [ActionTypes.LOGOUT_USER]({ dispatch }: ActionContext<S, R>): Promise<void>
  [ActionTypes.CLEAR_ALL_USER_INFORMATION]({ commit, dispatch }: ActionContext<S, R>): Promise<void>
}

export const actions: ActionTree<UserState, RootState> & Actions = {

  /**
   * 用户登录
   */
  async [ActionTypes.LOGIN_USER]({ commit, dispatch }, data: LoginDTO) {
    try {
      const form = encryption<LoginDTO>({
        data,
        param: ["password"],
      });
      const body = await loginReq(form)
      //TODO: Same Action 
      commit(MutationTypes.SET_USER_TOKEN, body)
      await dispatch(ActionTypes.GET_USER_INFORMATION)
      await dispatch(CommonActionTypes.GET_MENU)
      await router.push({
        path: defaultRoutePath,
      })
    } catch (error) {
      console.log(error);
    }
  },

  /**
   * 用户手机号登录
   */
  async [ActionTypes.LOGIN_USER_SMS]({ commit, dispatch }, data: SmsLoginDTO) {
    try {
      const form = { ...data }
      const body = await smsLoginReq(form)
      //TODO: Same Action 
      commit(MutationTypes.SET_USER_TOKEN, body)
      await dispatch(ActionTypes.GET_USER_INFORMATION)
      await dispatch(CommonActionTypes.GET_MENU)
      await router.push({
        path: defaultRoutePath,
      })
    } catch (error) {
      console.log(error);
    }
  },
  /**
   * 获取用户基本信息
   */
  async [ActionTypes.GET_USER_INFORMATION]({ commit }) {
    try {
      const body = await informationReq()
      commit(MutationTypes.SET_USERINFORMATION, body.data)
    } catch (error) {
      console.log(error);
    }
  },

  async [ActionTypes.LOGOUT_USER]({ dispatch }) {
    try {
      const { data, message, code } = await logoutReq()
      if (data) {
        console.log('用户登出成功');
      } else {
        console.log(message, code, data);
      }
    } catch (error) {
      console.log(error);
    } finally {
      dispatch(ActionTypes.CLEAR_ALL_USER_INFORMATION)
      router.push({
        path: loginRoutePath,
      })
    }
  },

  async [ActionTypes.CLEAR_ALL_USER_INFORMATION]({ commit, dispatch }) {
    commit(MutationTypes.CLEAR_USER_TOKEN)
    commit(MutationTypes.CLEAR_USERINFORMATION)
    dispatch(CommonActionTypes.CLEAR_MENU)
  },
}

